package com.api.gbhg.liuwenbo.api;

import cn.hutool.core.util.StrUtil;
import com.alibaba.fastjson.JSON;
import com.api.gbhg.liuwenbo.dao.DataBaseUtil;
import com.api.gbhg.daiyanlu.DataPrintCopyUtil.FormatUtil;
import com.api.gbhg.zengping.dao.PropertyManagementCostDao;
import com.api.gbhg.zengping.page.PageInfo;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import weaver.conn.RecordSet;
import weaver.conn.RecordSetTrans;

import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Path("/gb/Approval")
public class OfficeSuppliesApi {
    private static final Logger LOGGER = LoggerFactory.getLogger("gbhg");
    // 操作sql类
    private PropertyManagementCostDao rmcd=new PropertyManagementCostDao();

    private String tableName="uf_bgypbgjfbb";

    @POST
    @Path("/getOSData")
    @Produces(MediaType.APPLICATION_JSON)
    public String getOSData(Map<String,Object> map){
        if(map.isEmpty())
            return null;

        return getDataArray(map);
    }


    /**
     * 获取数据
     * @param maps
     * "state": 1, //流程状态值
     *  "tname": "uf_OfficeSupplies_vs",
     *  "pageNum": 0, //分页起始位
     *  "pageSize": 10 //分页数量
     * @return
     */
    public String getDataArray(Map<String,Object> maps){
        RecordSet rs = new RecordSet();
        String getsql = DataBaseUtil.getOSDataByrustId((String) maps.get("requestId"), rs.getDBType());
        rs.executeQuery(getsql);

        // 得到列名
        String[] columnName = rs.getColumnName();
        Map<String,Object>[] map = new HashMap[rs.getArray().size()];
        int i = 0;
        while (rs.next()){
            map[i]=new HashMap<String,Object>();
            for (String name: columnName) {
                if(name.equals("fzr")){
                    //编报人
                    RecordSet jbr = rmcd.getUserNameById(rs.getInt("fzr"));
                    jbr.next();
                    String lastname = jbr.getString("lastname");
                    if(StrUtil.isEmpty(lastname)){
                        lastname="系统管理员";
                    }
                    map[i].put("fzr",lastname);
                }else if(name.equals("sbdw")){
                    //查申报单位
                    RecordSet sbdw = rmcd.getSBDWById(rs.getInt("sbdw"));
                    sbdw.next();
                    map[i].put("sbdw",sbdw.getString("SUBCOMPANYNAME"));
                }else if(name.equals("sbks")){
                    // 查申报科室
                    RecordSet sbks = rmcd.getSBKSById(rs.getInt("sbks"));
                    sbks.next();
                    map[i].put("sbks",sbks.getString("departmentmark"));
                }else if(name.equals("yszje")||name.equals("zfcgyswy")||name.equals("hwgz")||name.equals("gc")||name.equals("fw")||name.equals("xecg")){
                    map[i].put(name, new BigDecimal(rs.getDouble(name)).divide(new BigDecimal("10000"),4, BigDecimal.ROUND_HALF_UP) );
                } else{
                    System.out.println(name+"--"+rs.getString(name));
                    map[i].put(name,rs.getString(name));
                }
            }
            i++;
        }
        PageInfo<String> pageInfo=new PageInfo();
        /*int pageCount = DataBaseUtil.getDataCount(maps.get("status").toString(), tableName);
        pageInfo.setPageNum(pageNum);
        pageInfo.setPageSize(pageSize);
        pageInfo.setTotal(pageCount);*/
        pageInfo.setList(map);
        pageInfo.setFromTable(tableName);
        return JSON.toJSONString(pageInfo);
    }


    /**
     *  获取sql
     * @param state 流程状态值
     * @param pageNum 分页起始位置
     * @param pageSize 需要数据条数
     * @param dbType 数据库类型
     * @return
     */
    public String getsql(Integer state,Integer pageNum,Integer pageSize,String dbType){
        StringBuilder sb = new StringBuilder();
        String sql=" SELECT t.id,v.lcjlsqjlid,v.xmmin,v.sbdw,v.sbks,v.fzr,v.yszje,t.zfcgyswy,t.hwgz, \n" +
                "t.gc,t.fw,t.xecg,SUM(t1.bzs) bzs,SUM(t1.xgyrs) xgyrs,SUM(t1.bzbhtgrs) bzbhtgrs \n" +
                "FROM uf_jingfei_version v join uf_bgypbgjfbb t on v.lcjlsqjlid = t.requestId \n" +
                "join uf_bgypbgjfbb_dt1 t1 on t.id = t1.mainid where ";
        sb.append(sql);

        if(state!=null && state == 3){
            sb.append("  v.lcztz = 3  ");
        }else{
            sb.append("  v.lcztz != 3 ");
        }
        sb.append(" group by v.Id,t.id,v.lcjlsqjlid,v.xmmin,v.sbdw,v.sbks,v.fzr,v.yszje,t.zfcgyswy,t.requestId,t.hwgz,t.gc,t.fw,t.xecg ");
        pageNum=(pageNum-1)*pageSize;
        if("sqlserver".equals(dbType)){
            sb.append(" order by v.Id offset "+pageNum+" rows fetch next "+pageSize+" rows only");
        }else if("mysql".equals(dbType)){
            sb.append(" LIMIT "+pageNum+","+pageSize+"");
        }
        LOGGER.info(sb.toString());
        return sb.toString();
    }
}
